﻿using AMAP.Domain;
using AMAP.ServiceClasses.Common;
using AMAP.ServiceInterfaces;
using AMAP.ServiceInterfaces.Common;
using System;
using System.Globalization;
using System.Linq;
using System.Threading;

namespace AMAP.ServiceClasses
{
    public class BasketService : BaseRepositoryService<Basket>, IBasketService
    {
        public BasketService(IDatabaseFactory databaseFactory)
            : base(databaseFactory)
        {
        }

        public int GetNumberOfBasket(DateTime Week)
        {
            int CurrentDayOfWeek = (int)Week.DayOfWeek - 1;
            if (CurrentDayOfWeek < 0)
                CurrentDayOfWeek = 6;
            DateTime LowLimit = Week.AddDays(-1 * CurrentDayOfWeek);
            LowLimit = DateTime.Parse(LowLimit.ToShortDateString());
            DateTime HighLimit = Week.AddDays(7 - CurrentDayOfWeek);
            LowLimit = DateTime.Parse(HighLimit.ToShortDateString());
            int QuarterIndex = (int)(Thread.CurrentThread.CurrentCulture.Calendar.GetMonth(Week) / 3);

            int BasketCount = 0;
            try { BasketCount += this.dataContext.BasketOrderSet.Where(b => b.CreationDate >= LowLimit && b.CreationDate < HighLimit).Sum(b => b.Quantity); }
            catch (Exception) { }
            try { BasketCount += this.dataContext.SubscriptionSet.Where(s => s.Quarter.Year == Week.Year && s.Quarter.Index == QuarterIndex).Sum(s => s.Quantity); }
            catch (Exception) { }

            return BasketCount;
        }

        public int GetNumberOfBasket(DateTime Week, DayOfWeek DayOfWeek)
        {

            int CurrentDayOfWeek = (int)Week.DayOfWeek - 1;
            if (CurrentDayOfWeek < 0)
                CurrentDayOfWeek = 6;
            DateTime LowLimit = Week.AddDays(-1 * CurrentDayOfWeek);
            LowLimit = DateTime.Parse(LowLimit.ToShortDateString());
            DateTime HighLimit = Week.AddDays(7 - CurrentDayOfWeek);
            LowLimit = DateTime.Parse(HighLimit.ToShortDateString());
            int QuarterIndex = (int)(Thread.CurrentThread.CurrentCulture.Calendar.GetMonth(Week) / 3);

            int BasketCount = 0;
            try { BasketCount += this.dataContext.BasketOrderSet.Where(b => b.CreationDate >= LowLimit && b.CreationDate < HighLimit && b.Client.Place.DeliveryDay == (int)DayOfWeek).Sum(b => b.Quantity); }
            catch (Exception) { }
            try { BasketCount += this.dataContext.SubscriptionSet.Where(s => s.Quarter.Year == Week.Year && s.Quarter.Index == QuarterIndex && s.Client.Place.DeliveryDay == (int)DayOfWeek).Sum(s => s.Quantity); }
            catch (Exception) { }

            return BasketCount;
        }
        public Basket GetByWeek(DateTime Week)
        {
            CultureInfo Culture = Thread.CurrentThread.CurrentCulture;
            int WeekIndex = (int)Culture.Calendar.GetWeekOfYear(Week, Culture.DateTimeFormat.CalendarWeekRule, Culture.DateTimeFormat.FirstDayOfWeek);
            return this.dataContext.BasketSet.FirstOrDefault(b => b.Week == WeekIndex && b.Year == Week.Year);
        }
    }
}
